package com.example.ezexcel.util;


import freemarker.core.XMLOutputFormat;
import freemarker.template.Configuration;
import freemarker.template.Template;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;

public class FileUtils {

    //配置信息,代码本身写的还是很可读的,就不过多注解了
    private static Configuration configuration = null;

    private static final String templateFolder = "D:\\excelToWord\\file";

    static {
        configuration = new Configuration(Configuration.VERSION_2_3_30);
        configuration.setDefaultEncoding("utf-8");
        configuration.setOutputFormat(XMLOutputFormat.INSTANCE);

        try {
            System.out.println(templateFolder);
            configuration.setDirectoryForTemplateLoading(new File(templateFolder));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 使用 freemarker 生成word文档
     *
     * @param templateName 模板 例如：xxx.ftl
     * @param data         数据
     * @param fileSavePath 文档生成后，存放的路径
     * @param filename     生成后的文件名称，使用英文
     * @param response
     * @throws Exception
     */
    public static void freemarkerExport(String templateName, Map<String, Object> data, String fileSavePath, String filename, HttpServletResponse response) throws Exception {

        // 3.设置 freeMarker生成Word文档所需要的模板 ---> xxx.ftl
        Template t = null;
        try {
            // 模板文件名称
            t = configuration.getTemplate(templateName,"utf-8");
        } catch (IOException e) {
            throw new IOException("获取 ftl模板失败!" + e.getMessage());
        }

        // 4.生成 Word文档的全路径名称
        File outFile = new File(fileSavePath + filename);

        // 5.创建一个 Word文档的输出流
        Writer writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream(outFile), "utf-8");
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }

        try {
            // 6.装载数据
            t.process(data, writer);
//
//            response.setCharacterEncoding("utf-8");
//            response.addHeader("Content-Disposition", "attachment;filename=" + filename);
//            response.setContentType("application/force-download");
//
//            // 7.读取生成好的 Word文档
//            File file = new File(fileSavePath + filename);
//            FileInputStream is = new FileInputStream(file);
//            OutputStream os = response.getOutputStream();
//            byte[] b = new byte[1024];
//            int length;
//            while ((length = is.read(b)) > 0) {
//                os.write(b, 0, length);
//            }
//            os.flush();
//            os.close();
            writer.flush();
            writer.close();
        } catch (IOException e) {
            throw new IOException(e.getMessage());
        } finally {
//            deleteTempFile(fileSavePath + filename);
        }
    }

    /**
     * 删除临时生成的文件
     */
    public static void deleteTempFile(String filePath) {
        File f = new File(filePath);
        f.delete();
    }

}
